import '../utils/preference_utils.dart';
import '../app_logger.dart';

/// 翻译设置管理器
/// 负责保存和加载用户的翻译偏好设置
class TranslationSettings {
  // 缓存键名
  static const String _prefKeyAutoPlayTts = 'translation_auto_play_tts';
  static const String _prefKeyUseMaleVoice = 'translation_use_male_voice';
  static const String _prefKeyOnlyShowTranslation = 'translation_only_show_translation';
  static const String _prefKeyAutoCopy = 'translation_auto_copy';
  static const String _prefKeyFromLanguage = 'translation_from_language';
  static const String _prefKeyToLanguage = 'translation_to_language';
  
  // 默认值
  static const bool _defaultAutoPlayTts = true;
  static const bool _defaultUseMaleVoice = false;
  static const bool _defaultOnlyShowTranslation = false;
  static const bool _defaultAutoCopy = true;
  static const String _defaultFromLanguage = 'zh-CN';
  static const String _defaultToLanguage = 'en-US';
  
  static TranslationSettings? _instance;
  static TranslationSettings get instance => _instance ??= TranslationSettings._();
  
  TranslationSettings._();
  
  /// 自动播放TTS
  bool get autoPlayTts => Prefs.getBool(_prefKeyAutoPlayTts, _defaultAutoPlayTts) ?? _defaultAutoPlayTts;
  set autoPlayTts(bool value) {
    Prefs.setBool(_prefKeyAutoPlayTts, value);
    log.d('💾 保存自动播放设置: $value');
  }
  
  /// 使用男声
  bool get useMaleVoice => Prefs.getBool(_prefKeyUseMaleVoice, _defaultUseMaleVoice) ?? _defaultUseMaleVoice;
  set useMaleVoice(bool value) {
    Prefs.setBool(_prefKeyUseMaleVoice, value);
    log.d('💾 保存男声设置: $value');
  }
  
  /// 仅显示译文
  bool get onlyShowTranslation => Prefs.getBool(_prefKeyOnlyShowTranslation, _defaultOnlyShowTranslation) ?? _defaultOnlyShowTranslation;
  set onlyShowTranslation(bool value) {
    Prefs.setBool(_prefKeyOnlyShowTranslation, value);
    log.d('💾 保存仅显示译文设置: $value');
  }
  
  /// 自动复制
  bool get autoCopy => Prefs.getBool(_prefKeyAutoCopy, _defaultAutoCopy) ?? _defaultAutoCopy;
  set autoCopy(bool value) {
    Prefs.setBool(_prefKeyAutoCopy, value);
    log.d('💾 保存自动复制设置: $value');
  }
  
  /// 源语言
  String get fromLanguage => Prefs.getString(_prefKeyFromLanguage, _defaultFromLanguage) ?? _defaultFromLanguage;
  set fromLanguage(String value) {
    Prefs.setString(_prefKeyFromLanguage, value);
    log.d('💾 保存源语言设置: $value');
  }
  
  /// 目标语言
  String get toLanguage => Prefs.getString(_prefKeyToLanguage, _defaultToLanguage) ?? _defaultToLanguage;
  set toLanguage(String value) {
    Prefs.setString(_prefKeyToLanguage, value);
    log.d('💾 保存目标语言设置: $value');
  }
  
  /// 获取所有设置的摘要
  Map<String, dynamic> getAllSettings() {
    return {
      'autoPlayTts': autoPlayTts,
      'useMaleVoice': useMaleVoice,
      'onlyShowTranslation': onlyShowTranslation,
      'autoCopy': autoCopy,
      'fromLanguage': fromLanguage,
      'toLanguage': toLanguage,
    };
  }
  
  /// 重置所有设置为默认值
  Future<void> resetToDefaults() async {
    await Prefs.setBool(_prefKeyAutoPlayTts, _defaultAutoPlayTts);
    await Prefs.setBool(_prefKeyUseMaleVoice, _defaultUseMaleVoice);
    await Prefs.setBool(_prefKeyOnlyShowTranslation, _defaultOnlyShowTranslation);
    await Prefs.setBool(_prefKeyAutoCopy, _defaultAutoCopy);
    await Prefs.setString(_prefKeyFromLanguage, _defaultFromLanguage);
    await Prefs.setString(_prefKeyToLanguage, _defaultToLanguage);
    log.d('🔄 翻译设置已重置为默认值');
  }
  
  /// 导出设置（用于备份）
  Map<String, dynamic> exportSettings() {
    final settings = getAllSettings();
    log.d('📤 导出翻译设置: $settings');
    return settings;
  }
  
  /// 导入设置（用于恢复）
  Future<void> importSettings(Map<String, dynamic> settings) async {
    try {
      if (settings.containsKey('autoPlayTts')) {
        autoPlayTts = settings['autoPlayTts'] ?? _defaultAutoPlayTts;
      }
      if (settings.containsKey('useMaleVoice')) {
        useMaleVoice = settings['useMaleVoice'] ?? _defaultUseMaleVoice;
      }
      if (settings.containsKey('onlyShowTranslation')) {
        onlyShowTranslation = settings['onlyShowTranslation'] ?? _defaultOnlyShowTranslation;
      }
      if (settings.containsKey('autoCopy')) {
        autoCopy = settings['autoCopy'] ?? _defaultAutoCopy;
      }
      if (settings.containsKey('fromLanguage')) {
        fromLanguage = settings['fromLanguage'] ?? _defaultFromLanguage;
      }
      if (settings.containsKey('toLanguage')) {
        toLanguage = settings['toLanguage'] ?? _defaultToLanguage;
      }
      log.d('📥 导入翻译设置完成: $settings');
    } catch (e) {
      log.d('❌ 导入翻译设置失败: $e');
    }
  }
  
  /// 检查是否是首次使用（用于显示引导）
  bool get isFirstTime => Prefs.getBool('translation_first_time') ?? true;
  
  /// 标记不是首次使用
  void markNotFirstTime() {
    Prefs.setBool('translation_first_time', false);
    log.d('✅ 标记翻译设置首次使用完成');
  }
}